using System;
using System.Collections.Generic;
using UnityEngine;
using Voodoo.Pattern;

public class ZoningManager : MonoBehaviour
{
	[Serializable]
	public class ZoningDayConfig
	{
		public Transform SkyPrefab;

		public Transform GroundPrefab;
	}

	[Serializable]
	public class ZoningCycleConfig
	{
		public List<ZoningDayConfig> DayConfigs;
	}

	public const string ZONING_PREF = "VoodooZoningIndex";

	public List<ZoningCycleConfig> Configs;

	public List<GameObject> ControlElements;

	private Transform _currentSky;

	private Transform _currentGround;

	private int lastLoaded = -1;

	public int RoundIndex
	{
		get
		{
			return NintendoPlayerPrefs.GetInt("VoodooZoningIndex", 0);
		}
		set
		{
			NintendoPlayerPrefs.SetInt("VoodooZoningIndex", value);
		}
	}

	private void Start()
	{
		if (Preloader.EnvironmentConfig.IsEnabled)
		{
			for (int i = 0; i < ControlElements.Count; i++)
			{
				UnityEngine.Object.Destroy(ControlElements[i]);
			}
		}
		Refresh();
		GameManager instance = SingletonMB<GameManager>.Instance;
		instance.OnGameEnd = (Action)Delegate.Combine(instance.OnGameEnd, new Action(OnGameFinished));
	}

	private void OnDestroy()
	{
		if ((bool)SingletonMB<GameManager>.Instance)
		{
			GameManager instance = SingletonMB<GameManager>.Instance;
			instance.OnGameEnd = (Action)Delegate.Remove(instance.OnGameEnd, new Action(OnGameFinished));
		}
	}

	public void OnGameFinished()
	{
		RoundIndex++;
		Refresh();
	}

	public void Refresh()
	{
		if (!Preloader.EnvironmentConfig.IsEnabled)
		{
			return;
		}
		int index = NintendoPlayerPrefs.GetInt("Level", 0) % Configs.Count;
		ZoningCycleConfig zoningCycleConfig = Configs[index];
		int num = Mathf.FloorToInt((float)RoundIndex / 4f) % zoningCycleConfig.DayConfigs.Count;
		if (Mathf.FloorToInt((float)RoundIndex / 4f) >= zoningCycleConfig.DayConfigs.Count)
		{
			RoundIndex = 0;
		}
		ZoningDayConfig zoningDayConfig = zoningCycleConfig.DayConfigs[num];
		if (lastLoaded != num)
		{
			lastLoaded = num;
			if (_currentGround != null)
			{
				UnityEngine.Object.Destroy(_currentGround.gameObject);
			}
			if (_currentSky != null)
			{
				UnityEngine.Object.Destroy(_currentSky.gameObject);
			}
			_currentGround = UnityEngine.Object.Instantiate(zoningDayConfig.GroundPrefab);
			_currentSky = UnityEngine.Object.Instantiate(zoningDayConfig.SkyPrefab);
		}
	}
}
